#在aws中新建一个服务器上，创建fdfs服务器的脚本，注意要传入ip
#! /bin/sh

if [ ! -n "$1" ] ;then

    echo "you have not input a IP!"
else

    echo "the IP you input is $1"

	user root;
	echo '####################开始FastDfs自动部署####################'
	basePath=/usr/local ##指定安装包的位置

	sudo yum -y install wget 
	sudo yum -y install gcc gcc-c++
	sudo yum -y install perl
	#部署libfastcommon包
	cd $basePath
	if [ ! -f "V1.0.7.tar.gz" ]; then
		sudo wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
	fi
	if [ ! -f "libfastcommon-1.0.7" ]; then
		sudo tar -zxf V1.0.7.tar.gz
	fi
	cd libfastcommon-1.0.7
	sudo ./make.sh
	sudo ./make.sh install
	#创建软链接
	sudo rm -rf /usr/local/lib/libfastcommon.so /usr/lib/libfastcommon.so /usr/local/lib/libfdfsclient.so /usr/lib/libfdfsclient.so
	sudo ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
	sudo ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
	sudo ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
	sudo ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

	#部署fastdfs-5.05包
	cd $basePath
	if [ ! -f "V5.05.tar.gz" ]; then
		sudo wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
	fi
	if [ ! -f "fastdfs-5.05" ]; then
		sudo tar -zxf V5.05.tar.gz
	fi
	cd fastdfs-5.05
	sudo ./make.sh
	sudo ./make.sh install
	#复制
	cd /etc/fdfs
	sudo rm -rf tracker.conf storage.conf client.conf
	sudo cp tracker.conf.sample tracker.conf
	sudo cp storage.conf.sample storage.conf
	sudo cp client.conf.sample client.conf
	#替换tracker.conf
	sudo sed -i "s/^bind_addr.*/bind_addr=/g" /etc/fdfs/tracker.conf
	sudo sed -i "s/^base_path=.*/base_path=\/fastdfs\/tracker/g" /etc/fdfs/tracker.conf
	sudo sed -i "s/^port=22122.*/port=22122/g" /etc/fdfs/tracker.conf
	sudo sed -i "s/^http.server_port=.*/http.server_port=8080/g" /etc/fdfs/tracker.conf
	#替换storage.conf
	sudo sed -i "s/^group_name=.*/group_name=files/g" /etc/fdfs/storage.conf
	sudo sed -i "s/^bind_addr=.*/bind_addr=/g" /etc/fdfs/storage.conf
	sudo sed -i "s/^port=23000.*/port=23000/g" /etc/fdfs/storage.conf
	sudo sed -i "s/^base_path=.*/base_path=\/fastdfs\/storage\/info/g" /etc/fdfs/storage.conf
	sudo sed -i "s/^store_path0=.*/store_path0=\/fastdfs\/storage/g" /etc/fdfs/storage.conf
	sudo sed -i "s/^tracker_server=.*/tracker_server=$1:22122/g" /etc/fdfs/storage.conf
	sudo sed -i "s/^http.server_port=.*/http.server_port=8888/g" /etc/fdfs/storage.conf
	#创建目录
	sudo rm -rf /fastdfs
	sudo mkdir -p /fastdfs/tracker
	sudo mkdir -p /fastdfs/storage/info

	#启动tracker与storage
	sudo fdfs_trackerd  /etc/fdfs/tracker.conf start
	sudo fdfs_storaged  /etc/fdfs/storage.conf start

	#部署fastdfs-nginx-module_v1.16
	cd $basePath
	if [ ! -f "fastdfs-nginx-module_v1.16.tar.gz" ]; then
		sudo wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
	fi
	sudo rm -rf fastdfs-nginx-module
	sudo tar -zxf fastdfs-nginx-module_v1.16.tar.gz
	cd /usr/local/fastdfs-nginx-module/src
	#替换config
	sudo sed -i "s/^CORE_INCS=.*/CORE_INCS=\"\$CORE_INCS \/usr\/include\/fastdfs \/usr\/include\/fastcommon\/\"/g" /usr/local/fastdfs-nginx-module/src/config
	#复制mod_fastdfs.conf、http.conf、mime.types
	sudo cp  /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
	sudo cp /usr/local/fastdfs-5.05/conf/http.conf  /etc/fdfs/
	sudo cp /usr/local/fastdfs-5.05/conf/mime.types  /etc/fdfs/
	#创建目录
	sudo mkdir -p /fastdfs/nginxlog
	#替换mod_fastdfs.conf
	sudo sed -i "s/^group_name=.*/group_name=files/g" /etc/fdfs/mod_fastdfs.conf
	sudo sed -i "s/^tracker_server=.*/tracker_server=$1:22122/g" /etc/fdfs/mod_fastdfs.conf
	sudo sed -i "s/^store_path0=.*/store_path0=\/fastdfs\/storage/g" /etc/fdfs/mod_fastdfs.conf
	sudo sed -i "s/^base_path=.*/base_path=\/fastdfs\/nginxlog/g" /etc/fdfs/mod_fastdfs.conf
	sudo sed -i "s/^url_have_group_name.*/url_have_group_name=true/g" /etc/fdfs/mod_fastdfs.conf
	#替换client.conf
	sed -i "s/^tracker_server=.*/tracker_server=$1:22122/g" /etc/fdfs/client.conf
	sed -i "s/^base_path=.*/base_path=\/fastdfs\/storage/g" /etc/fdfs/client.conf
	cd /fastdfs
	sudo ln -s /fastdfs/storage/data /fastdfs/data

	#部署pcre-8.36.tar.gz
	cd $basePath
	if [ ! -f "pcre-8.36.tar.gz" ]; then
		sudo wget https://ftp.pcre.org/pub/pcre/pcre-8.36.tar.gz
	fi
	sudo tar -zxvf pcre-8.36.tar.gz
	cd pcre-8.36
	sudo ./configure
	sudo make 
	sudo make install
	sudo ln -s /usr/local/lib/libpcre.so.1 /lib64/	

	#部署zlib-1.2.11.tar.gz
	cd $basePath
	if [ ! -f "zlib-1.2.11.tar.gz" ]; then
		sudo wget http://zlib.Net/zlib-1.2.11.tar.gz
	fi
	sudo tar -zxvf zlib-1.2.11.tar.gz
	cd zlib-1.2.11
	sudo ./configure
	sudo make 
	sudo make install

	#部署nginx-1.8.0.tar.gz
	basePath=/usr/local
	cd $basePath
	if [ ! -f "nginx-1.8.0.tar.gz" ]; then
		sudo wget http://nginx.org/download/nginx-1.8.0.tar.gz
	fi
	sudo tar -zxvf nginx-1.8.0.tar.gz
	cd nginx-1.8.0
	sudo ./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module/src
	sudo make
	sudo make install
	#替换nginx.conf
	cd /usr/local/nginx/conf
	sudo sed -i '47a         location /files/M00{\n            root /fastdfs/data;\n            ngx_fastdfs_module;\n        }' /usr/local/nginx/conf/nginx.conf
	#替换nginx的端口为8345
	sudo sed -i '36s/80/8345/' /usr/local/nginx/conf/nginx.conf
	sudo /usr/local/nginx/sbin/nginx
	echo '####################FastDfs自动部署完成####################'

fi

#重新启动fdfs
#	/usr/local/nginx/sbin/nginx -s reload 如果报错 要用 /usr/local/nginx/sbin/nginx
#	/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
#  /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

